﻿using L.Tool;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace L.UI.Base
{
    public class BasePanel : MonoBehaviour
    {
        #region 组件字典

        [HideInInspector]
        public List<GameObject> components = new List<GameObject>();
        Dictionary<string, GameObject> m_Objects = new Dictionary<string, GameObject>();
        Dictionary<string, Button> m_Button = new Dictionary<string, Button>();
        Dictionary<string, Text> m_Text = new Dictionary<string, Text>();
        Dictionary<string, Image> m_Image = new Dictionary<string, Image>();
        Dictionary<string, Sprite> m_Sprite = new Dictionary<string, Sprite>();
        Dictionary<string, Toggle> m_Toggle = new Dictionary<string, Toggle>();
        Dictionary<string, Slider> m_Slider = new Dictionary<string, Slider>();
        Dictionary<string, InputField> m_InputField = new Dictionary<string, InputField>();
        //折线统计图
        //Dictionary<string, LineChart> m_Line = new Dictionary<string, LineChart>();

        #endregion

        #region 公共字段
        [Header("UI面板类型")]
        public UIPanelType uipt = UIPanelType.MainPanel;

        #endregion

        #region 周期函数
        /// <summary>
        /// 在UIManager中注册当前UI
        /// </summary>
        protected virtual void Awake()
        {
            UIManager.Instance.AddCacheUIs(this, uipt);
        }
        #endregion

        #region 显示关闭
        /// <summary>
        /// 打开UI窗体
        /// </summary>
        /// <param name="UIName">打开窗体名称</param>
        protected void OpenUI(UIPanelType uIPanelType ,Action initUi = null)
        {
            UIManager.Instance.ShowUI(uIPanelType);
        }


        /// <summary>
        /// 关闭UI窗体
        /// </summary>
        protected void CloseUI()
        {
            UIManager.Instance.CloseUI(uipt);
        }

        /// <summary>
        /// 显示当前窗口
        /// </summary>
        public virtual void OnActiveTrue()
        {
            CanvasGroup canvasGroup = gameObject.GetComponent<CanvasGroup>();
            canvasGroup.alpha = 1;
            canvasGroup.interactable = true;
            canvasGroup.blocksRaycasts = true;
        }
        /// <summary>
        /// 关闭当前窗口
        /// </summary>
        public virtual void OnActiveFalse()
        {
            CanvasGroup canvasGroup = gameObject.GetComponent<CanvasGroup>();
            canvasGroup.alpha = 0;
            canvasGroup.interactable = false;
            canvasGroup.blocksRaycasts = false;
        }

        /// <summary>
        /// 关闭程序
        /// </summary>
        public void CloseProcedure()
        {
            Application.Quit();
        }

        /// <summary>
        /// 冻结状态
        /// </summary>
        public virtual void OnFreeze()
        {
            this.OnActiveTrue();
        }

        /// <summary>
        /// 恢复到初始UI
        /// </summary>
        public virtual void InitUI()
        {
            
        }
        #endregion

        #region 获取组件
        /// <summary>
        /// 获取GameObject
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public GameObject GetGameObject(string name)
        {
            if (m_Objects.ContainsKey(name))
                return m_Objects[name];
            GameObject temp = UnityHelper.Find(gameObject.transform, name).gameObject;
            if (temp == null)
            {
                Debug.LogError("不存在名称为" + name + "的GameObject组件");
            }
            m_Objects.Add(name, temp);
            return temp;
        }

        /// <summary>
        /// 获取Button
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Button GetButton(string name)
        {
            if (m_Button.ContainsKey(name))
                return m_Button[name];
            Button temp = UnityHelper.Find(gameObject.transform, name).GetComponent<Button>();
            if (temp == null)
            {
                Debug.LogError("不存在名称为" + name + "的Button组件");
            }
            m_Button.Add(name, temp);
            return temp;
        }

        /// <summary>
        /// 获取Toggle
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Toggle GetToggle(string name)
        {
            if (m_Toggle.ContainsKey(name))
                return m_Toggle[name];
            Toggle temp = UnityHelper.Find(gameObject.transform, name).GetComponent<Toggle>();
            if (temp == null)
            {
                Debug.LogError("不存在名称为" + name + "的Toggle组件");
            }
            m_Toggle.Add(name, temp);
            return temp;
        }

        /// <summary>
        /// 获取Slider
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Slider GetSlider(string name)
        {
            if (m_Slider.ContainsKey(name))
                return m_Slider[name];
            Slider temp = UnityHelper.Find(gameObject.transform, name).GetComponent<Slider>();
            if (temp == null)
            {
                Debug.LogError("不存在名称为" + name + "的Slider组件");
            }
            m_Slider.Add(name, temp);
            return temp;
        }

        /// <summary>
        /// 获取text
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Text GetText(string name)
        {
            if (m_Text.ContainsKey(name))
                return m_Text[name];
            Text temp = UnityHelper.Find(gameObject.transform, name).GetComponent<Text>();
            if (temp == null)
            {
                Debug.LogError("不存在名称为" + name + "的Text组件");
            }
            m_Text.Add(name, temp);
            return temp;
        }

        /// <summary>
        /// 获取Image
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Image GetImage(string name)
        {
            if (m_Image.ContainsKey(name))
                return m_Image[name];
            Image temp = UnityHelper.Find(gameObject.transform, name).GetComponent<Image>();
            if (temp == null)
            {
                Debug.LogError("不存在名称为" + name + "的Image组件");
            }
            m_Image.Add(name, temp);
            return temp;
        }

        /// <summary>
        /// 获取Sprite
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Sprite GetSprite(string name)
        {
            if (m_Sprite.ContainsKey(name))
                return m_Sprite[name];
            Sprite temp = UnityHelper.Find(gameObject.transform, name).GetComponent<Sprite>();
            if (temp == null)
            {
                Debug.LogError("不存在名称为" + name + "的Sprite组件");
            }
            m_Sprite.Add(name, temp);
            return temp;
        }


        /// <summary>
        /// 获取InputField
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public InputField GetInputField(string name)
        {
            if (m_InputField.ContainsKey(name))
                return m_InputField[name];
            InputField temp = UnityHelper.Find(gameObject.transform, name).GetComponent<InputField>();
            if (temp == null)
            {
                Debug.LogError("不存在名称为" + name + "的InputField组件");
            }
            m_InputField.Add(name, temp);
            return temp;
        }

        /// <summary>
        /// 获取折线统计图
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        //public LineChart GetLineChat(string name)
        //{
        //    if (m_Line.ContainsKey(name))
        //        return m_Line[name];
        //    LineChart temp = GetGameObject(name).GetComponent<LineChart>();
        //    if (temp == null)
        //    {
        //        Debug.LogError("不存在名称为" + name + "的Text组件");
        //    }
        //    m_Line.Add(name, temp);
        //    return temp;
        //}
        

        #endregion

        #region Buttton方法
        /// <summary>
        /// 设置按钮点击的事件
        /// </summary>
        /// <param name="name">按钮的名字</param>
        /// <param name="action">事件</param>
        /// <param name="str">string 类型参数</param>
        public void AddOnClickListener(string name, Action<string> action, string str = null)
        {
            GetButton(name).onClick.AddListener(delegate {
                action(str);
            });
        }
        public void AddOnClickListener(string name, Action<string> action)
        {
            GetButton(name).onClick.AddListener(delegate {
                action(name);
            });
        }
        /// <summary>
        /// 设置按钮点击的事件
        /// </summary>
        /// <param name="name">按钮的名字</param>
        /// <param name="action">事件</param>
        /// <param name="str">string 类型参数</param>
        public void AddOnClickListener(string name, Action<bool> action, bool state = false)
        {
            GetButton(name).onClick.AddListener(delegate {
                action(state);
            });
        }

        /// <summary>
        /// 设置按钮点击的事件
        /// </summary>
        /// <param name="name">按钮的名字</param>
        /// <param name="action">事件</param>
        /// <param name="str">string 类型参数</param>
        /// <param name="v">bool 类型参数</param>
        public void AddOnClickListener(string name, Action<string, bool> action, string str = null, bool v = true)
        {
            GetButton(name).onClick.AddListener(delegate {
                action(str, v);
            });
        }

        /// <summary>
        /// 设置按钮点击的事件
        /// </summary>
        /// <param name="name">按钮的名字</param>
        /// <param name="action">事件</param>
        /// <param name="str">string 类型参数</param>
        /// <param name="str1">string 类型参数</param>
        /// <param name="str2">string 类型参数</param>
        public void AddOnClickListener(string name, Action<string, string, string> action, string str = null, string str1 = null, string str2 = null)
        {
            GetButton(name).onClick.AddListener(delegate {
                action(str, str1, str2);
            });
        }
        /// <summary>
        /// 设置按钮点击的事件
        /// </summary>
        /// <param name="name">按钮的名字</param>
        /// <param name="action">事件</param>
        /// <param name="str">string 类型参数</param>
        /// <param name="str1">string 类型参数</param>
        /// <param name="str2">string 类型参数</param>
        public void AddOnClickListener(string name, Action<string, string, string, List<int>> action, string str = null, string str1 = null, string str2 = null, List<int> num = null)
        {
            GetButton(name).onClick.AddListener(delegate {
                action(str, str1, str2, num);
            });
        }

        /// <summary>
        /// 设置按钮点击的事件
        /// </summary>
        /// <param name="name">按钮的名字</param>
        /// <param name="action">事件</param>
        public void AddOnClickListener(string name, Action action)
        {
            GetButton(name).onClick.AddListener(delegate { action(); });
        }
        /// <summary>
        /// 设置按钮点击的事件
        /// </summary>
        /// <param name="name">按钮的名字</param>
        /// <param name="action">事件</param>
        public void AddOnClickListener(string name, Action<int,string> action, int num)
        {
            GetButton(name).onClick.AddListener(delegate { action(num,name); });
        }
        public void AddOnClickListener(string name, Action<int> action, int num)
        {
            GetButton(name).onClick.AddListener(delegate { action(num); });
        }

        /// <summary>
        /// 设置按钮点击的事件
        /// </summary>
        /// <param name="name">按钮的名字</param>
        /// <param name="action">事件</param>
        public void AddOnClickListenerToInputField(string name, Action<string, string> action, string str = null, string str1 = null)
        {
            GetButton(name).onClick.AddListener(delegate { action(str, str1); });
        }
        /// <summary>
        /// 设置按钮点击的事件
        /// </summary>
        /// <param name="name">按钮的名字</param>
        /// <param name="action">事件</param>
        public void AddOnClickListener(string name, Action<GameObject> action, GameObject obj)
        {
            GetButton(name).onClick.AddListener(delegate { action(obj); });
        }

        public void AddOnClickListener(GameObject gameObject, Vector3 vector3, float time, Action<GameObject, Vector3, float> action)
        {
            GetButton(name).onClick.AddListener(delegate { action(gameObject, vector3, time); });
        }
        #endregion

        #region Text方法
        /// <summary>
        /// 设置Text的内容
        /// </summary>
        /// <param name="name">Text名字</param>
        /// <param name="connet">Text内容</param>
        public void SetTextValue(string name, string connet)
        {
            GetText(name).text = connet;
        }
        #endregion

        #region Image方法

        #endregion

        #region Sprit方法

        #endregion

        #region Toggle方法
        /// <summary>
        /// 设置Toggle事件
        /// </summary>
        /// <param name="name">Slider名字</param>
        /// <param name="call">事件</param>
        public void AddToggleOnValueChange(string name, UnityEngine.Events.UnityAction<bool> call)
        {
            GetToggle(name).onValueChanged.AddListener(call);
        }
        #endregion

        #region Slider方法
        /// <summary>
        /// 设置Slider滑动事件
        /// </summary>
        /// <param name="name">Slider名字</param>
        /// <param name="callback">事件</param>
        public void AddOnValueChangeSlider(string name, UnityEngine.Events.UnityAction<float> callback, string parm = null)
        {
            GetSlider(name).onValueChanged.AddListener(callback);
        }
        #endregion

        #region InputField方法

        /// <summary>
        /// 清空inputfield的内容
        /// </summary>
        /// <param name="name"></param>
        public void InputFieldSetEmpty(string name)
        {
            GetInputField(name).text = "";
        }

        /// <summary>
        /// 设置inputfield的内容
        /// </summary>
        /// <param name="name"></param>
        public void SetInputFieldText(string name, string str)
        {
            GetInputField(name).text = str;
        }

        /// <summary>
        /// 限制inputfield的内容
        /// </summary>
        /// <param name="name"></param>
        public void LimitInputFieldText(string name, float min, float max)
        {
            GetInputField(name).onEndEdit.AddListener(delegate {
                string str = GetInputField(name).text;
                if (float.TryParse(str, out float num))
                {
                    if (num < min)
                    {
                        GetInputField(name).text = min.ToString();
                    }
                    else if (num > max)
                    {
                        GetInputField(name).text = max.ToString();
                    }
                }

            });
        }

        #endregion

        #region 表盘方法
        //public void AddUpdateGaugeData(string name, ref Func<int, int, double, bool> callback, string parm = null)
        //{
        //    GetGaugeChat(name).ClearData();
        //    callback += GetGaugeChat(name).UpdateData;
        //}
        #endregion

        #region 折线统计图方法
        /// <summary>
        /// 折线统计图方法
        /// </summary>
        /// <param name="name"></param>
        /// <param name="callback">serie，x轴名字，x值，y值</param>
        //protected void AddUpdateLineData(string name, ref Func<int, string, double, double, SerieData> callback)
        //{
        //    callback += GetLineChat(name).AddData;
        //}

        #endregion
    }

}